package com.tz.fruit.controller;

import com.tz.fruit.entity.*;
import com.tz.fruit.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by CHEN on 2017/7/19.
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService iProductService;
    @RequestMapping("/index")
    public String index(Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        PageBean<Product> page=iProductService.findByPage(null, null,null,null,true,1L,4L);
        PageBean<Product> page2=iProductService.findByPage(null, RecState.新品上市,null,null,true,1L,4L);
        PageBean<Product> page1=iProductService.findByPage(null, RecState.推荐产品,null,null,true,1L,4L);
        PageBean<Product> page3=iProductService.findByPage(null, RecState.热卖产品,null,null,true,1L,4L);
        model.addAttribute("page",page);
        model.addAttribute("page1",page1);
        model.addAttribute("page2",page2);
        model.addAttribute("page3",page3);
        session.setAttribute("user",user);
        return "index";
    }

    @RequestMapping("/list")
    public String list(Model model, String keyWord, @RequestParam(defaultValue = "-1")String cat,@RequestParam(defaultValue = "-1")String rec,@RequestParam(defaultValue = "-1")String loc,@RequestParam(defaultValue = "false") boolean order,@RequestParam(defaultValue = "1")Long pageNow,@RequestParam(defaultValue = "4")Long pageSize){
        RecState recState=null;
        if (!rec.equals("-1"))
            recState=Enum.valueOf(RecState.class,rec);

        Category category=null;
        if (!cat.equals("-1")) {
            category = new Category();
            category.setCategoryId(Long.valueOf(cat));
        }
        PageBean<Product> page=iProductService.findByPage(keyWord,recState,category,loc,order,pageNow,pageSize);
        List<Category> cats=iProductService.findAllCategory(null,null);
        model.addAttribute("categories",cats);
        model.addAttribute("keyWord",keyWord);
        model.addAttribute("rec",rec);
        model.addAttribute("cat",cat);
        model.addAttribute("loc",loc);
        model.addAttribute("order",order);
        model.addAttribute("page",page);
        return "list";
    }

    @RequestMapping("/names")
    public @ResponseBody List<String> names(){
        List<String> list=new ArrayList<>();
        List<Product> products=iProductService.findByPage(null,null,null,null,true,1l,99l).getList();
        for (Product p:products
             ) {
            list.add(p.getpName());
        }
        return list;
    }

    @RequestMapping("/details")
    public String details(Model model,Long id, HttpServletRequest req){
        List<Intro> intros=iProductService.findIntroByPid(id);
        List<Image> images=iProductService.findImgByPid(id);
        List<Comment> comments=iProductService.findCommentByPid(id);
        Product product=iProductService.findProductById(id);
        PageBean<Comment> page=iProductService.findCommentByPage(id,1L,5L);

        Recent recent= (Recent) req.getSession().getAttribute("recent");

        System.out.println("#####");
        recent=iProductService.saveToRecent(recent,product);
        req.getSession().setAttribute("recent",recent);

        model.addAttribute("intros",intros);
        model.addAttribute("images",images);
        model.addAttribute("comments",comments);
        model.addAttribute("page",page);
        model.addAttribute("product",product);

        return "details";
    }

    @RequestMapping("/details2")
    public String details2(Model model,String pNo, HttpServletRequest req){
        Long id=iProductService.findPidByPNo(pNo);
        List<Intro> intros=iProductService.findIntroByPid(id);
        List<Image> images=iProductService.findImgByPid(id);
        List<Comment> comments=iProductService.findCommentByPid(id);
        Product product=iProductService.findProductById(id);
        PageBean<Comment> page=iProductService.findCommentByPage(id,1L,5L);

        Recent recent= (Recent) req.getSession().getAttribute("recent");

        System.out.println("#####");
        recent=iProductService.saveToRecent(recent,product);
        req.getSession().setAttribute("recent",recent);

        model.addAttribute("intros",intros);
        model.addAttribute("images",images);
        model.addAttribute("comments",comments);
        model.addAttribute("page",page);
        model.addAttribute("product",product);

        return "details";
    }

    @RequestMapping("/comments")
    public @ResponseBody PageBean<Comment> comments(Long id,Long pageNow){
        return iProductService.findCommentByPage(id,pageNow,5L);
    }
}
